﻿using GalaSoft.MvvmLight;
using Cleverscape.UTorrentClient.WebClient;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace uTorrentSL.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class FeedsViewModel : MyBaseViewModel
    {
        public RelayCommand<Feed> SelectCommand { get; private set; }

        public RelayCommand<string> AddFeedCommand { get; set; }


        /// <summary>
        /// The <see cref="SelectedFeed" /> property's name.
        /// </summary>
        public const string SelectedFeedPropertyName = "SelectedFeed";

        private Feed _selectedFeed = null;

        /// <summary>
        /// Gets the SelectedFeed property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Feed SelectedFeed
        {
            get
            {
                return _selectedFeed;
            }

            set
            {
                if (_selectedFeed == value)
                {
                    return;
                }

                var oldValue = _selectedFeed;
                _selectedFeed = value;


                // Update bindings, no broadcast
                RaisePropertyChanged(SelectedFeedPropertyName);
            }
        }


        /// <summary>
        /// The <see cref="Feeds" /> property's name.
        /// </summary>
        public const string FeedsPropertyName = "Feeds";

        private FeedCollection _feeds = null;

        /// <summary>
        /// Gets the Feeds property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public FeedCollection Feeds
        {
            get
            {
                return _feeds;
            }

            set
            {
                if (_feeds == value)
                {
                    return;
                }

                var oldValue = _feeds;
                _feeds = value;

                // Remove one of the two calls below
                //throw new NotImplementedException();

                // Update bindings, no broadcast
                RaisePropertyChanged(FeedsPropertyName);

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                //RaisePropertyChanged(FeedsPropertyName, oldValue, value, true);
            }
        }


        /// <summary>
        /// Initializes a new instance of the FeedsViewModel class.
        /// </summary>
        public FeedsViewModel()
        {
            //this.Feeds = data.Feeds;
            ////if (IsInDesignMode)
            ////{
            ////    // Code runs in Blend --> create design time data.
            ////}
            ////else
            ////{
            ////    // Code runs "for real": Connect to service, etc...
            ////}

            SelectCommand = new RelayCommand<Feed>(f =>
            {
                Messenger.Default.Send<Feed>(f, "SetFeed");
                //Messenger.Default.Send<string>("FeedView", "LoadView");
                
            });

            AddFeedCommand = new RelayCommand<string>((name) =>
            {
                Feed newFeed = new Feed();
                newFeed.Alias = name;
                //this.Data.FilterUpdateAdd(newFilter);
                ViewModelLocator.CloseModal();
                SelectCommand.Execute(newFeed);
            });
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}